import React from 'react';
import { useSize } from 'ahooks';

// 计算一屏可容纳
export default (ref, { total, minWidth }) => {
  const size = useSize(ref); // 容器大小
  const [columnWidth, setColumnWidth] = React.useState(0); // 列宽
  const [pageSize, setPageSize] = React.useState(0); // 列数

  React.useEffect(() => {
    const pageWidth = size?.width ? size.width - 215 : 0;
    if (!pageWidth || !total) return;
    let _size;
    let cell_w = pageWidth / total;
    if (cell_w > minWidth) {
      _size = total;
    } else {
      _size = Math.floor(pageWidth / minWidth);
      cell_w = pageWidth / _size;
    }
    setColumnWidth(cell_w);
    setPageSize(_size);
  }, [size?.width, total]);

  return {
    columnWidth,
    pageSize,
    pageWidth: size?.width,
  };
};
